package com.luoqiwen.android.sinaurldecoder

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context.CLIPBOARD_SERVICE
import android.view.View
import android.view.View.OnClickListener
import android.widget.EditText
import android.widget.Toast
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

class ClickClipBtnListener(private val main: MainActivity) : OnClickListener {
    private val textView: EditText = main.findViewById<EditText>(R.id.decodeResult)
    private val clipMgr  = main.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager

    override fun onClick(v: View?) {
        MainScope().launch {
            main.debug("Button event detected. Now executing clipboard interact task...")
            val clipData = ClipData.newPlainText("转换结果", textView.text.toString())
            clipMgr.setPrimaryClip(clipData)
            Toast.makeText(main.applicationContext, "成功复制到剪贴板!", Toast.LENGTH_SHORT).show()
            main.debug("Clipboard replace task executed successfully")

        }
    }
}